﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityTypes;
using System.Data.Objects;
using System.Data.Objects.DataClasses;

namespace SI2_TrabalhoFinal
{
    public partial class RelatorioConsultas : Form
    {
        SI2FinalEntities entities = Connection.entities;

        public RelatorioConsultas()
        {
            InitializeComponent();
        }

        private void RelatorioConsultas_Load(object sender, EventArgs e)
        {
            var queryMedico = from m in entities.Pessoa.Include("Medico").Where(m => m.Medico.Count != 0)
                              select m;

            try
            {
                this.MedicoRelatorio.DisplayMember = "Nome";
                this.MedicoRelatorio.DataSource = ((ObjectQuery)queryMedico).Execute(MergeOption.AppendOnly);                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void CalcularRelatorio_Click(object sender, EventArgs e)
        {
            if (this.MedicoRelatorio.SelectedItem == null)
            {
                MessageBox.Show("Seleccione um Médico");
                return;
            }

            if (this.InitPeriodoRelatorio.Value >= DateTime.Now) 
            {
                MessageBox.Show("Data de Inicio Inválida");
                return;
            }
            if (this.FimPeriodoRelatorio.Value >= DateTime.Now)
            {
                MessageBox.Show("Data de Fim Inválida");
                return;
            }
            if (this.InitPeriodoRelatorio.Value>this.FimPeriodoRelatorio.Value)
            {
                MessageBox.Show("A Data de Fim não pode ser inferior à de inicio");
                return;
            }
            ObjectParameter nrConsultas = new ObjectParameter("nrConsultas",SqlDbType.Int);
            ObjectParameter montante = new ObjectParameter("montante",SqlDbType.Int);
            entities.RelatorioConsultas(((Pessoa)this.MedicoRelatorio.SelectedItem).NumeroSS,
                InitPeriodoRelatorio.Value.Date,
                FimPeriodoRelatorio.Value.Date,
                nrConsultas,
                montante);
            label5.Text = montante.Value.ToString()+ " €";
            label7.Text = nrConsultas.Value.ToString();
        }

        private void MedicoRelatorio_Enter(object sender, EventArgs e)
        {
            label5.Text = "";
            label7.Text = "";
        }

        private void InitPeriodoRelatorio_Enter(object sender, EventArgs e)
        {
            label5.Text = "";
            label7.Text = "";
        }

        private void FimPeriodoRelatorio_Enter(object sender, EventArgs e)
        {
            label5.Text = "";
            label7.Text = "";
        }
        
    }
}
